#!/usr/bin/env python
#########################################################################
## Application:	    KernelCheck
## File:	    ./KCDebug.py
## Author:	    Scott 'deepspring' Smith <contact.sjsmith@gmail.com>
## Last Edit:	    23/03/2009
## Version:	    0.0.1
##
## Purpose:
## ^^^^^^^^
## The primary purpose of this script is to provide debugging information
## for the KernelCheck development package.
##
## ======================================================================
## Revision Information
## ======================================================================
##
## 23/03/2009 : SS : 0.0.1 
##	-> Initial File Creation
## 23/03/2009 : SS : 0.0.2 
##	-> Major improvements
##
#########################################################################
##

## Imports
## ^^^^^^^
import os
import sys
from datetime import date

## Class Definition
## ^^^^^^^^^^^^^^^^
## Class Name:
##	KCDebug
##
## Class Variables:
##	file_name   # Name of the current log file
##
## Public Methods:
##	add_debug_msg(script, object_type, name, data)
##
## Private Methods:
##	NIL
##
class KCDebug():
    # Class variables
    file_name = ''	# Name of the log file
    file_path = ''	# Location where the log file will be created

    ## Function to initialize our class
    def __init__(self, fresh=False):
        ## Check to UID, make sure we have the right permissions
        if os.geteuid() != 0:
            print >> sys.stderr, "Error: Must be root to use this class.\n"
            sys.exit(1)

    	## Set the path were the log file will be stored
    	## Note the trailing "/"
    	self.file_path = '/var/log/'

    	## Generate a new file name
    	self.file_name = 'kernelcheck-debug-' + date.today().isoformat() + '.log'

    	## If we want a fresh log file
    	if fresh == True:
    	    ## Check to see if one already exists
    	    if os.path.isfile(self.file_path + self.file_name):
    		## If does, remove it
		os.remove(self.file_path + self.file_name)

    ## Method to add debug messages to the log file
    ##	    Arguments:
    ##		self		<== Points back to an object within the
    ##				        current class
    ##		script_name	<== Filename the object is set in, 
    ##				        I recommend using sys.argv[0]
    ##		object_name	<== Type of object (type(object))
    ##		object_contents	<== Data held by object
    def add_debug_msg(self, script_name, object_name, object_contents):
    	## Lets check if the file exists and set the file write mode accordingly
    	if os.path.isfile(self.file_path + self.file_name):
    	    file = open(self.file_path + self.file_name, 'a')
    	else:
    	    file = open(self.file_path + self.file_name, 'w')
    	
    	## Lets make sure we have a "file" and if we do lets start logging
    	if file:    
    	    ## Make a pretty block of ascii
    	    file.write('\n')
    	    file.write('*' * 74 + '\n')
    	    file.write('Script File:\t' + script_name + '\n')
    	    file.write('Object Name:\t' + object_name + '\n')
    	    file.write('Object Type:\t' + str(type(object_contents)) + '\n')
    	    file.write('Object Values...\n\n' + str(object_contents) + '\n\n')
    	    file.write('*' * 74 + '\n')

    	    ## Close the file when your done
    	    file.close()

    	else:
    	    print >> sys.stderr, "Error: Unable to open file - " + self.file_path + self.file_name + "\n"
    	    sys.exit(1)

    ## Method that returns the location of the log file (including its file name)
    ##	    Arguments:
    ##		self		<== Points back to an object within the
    ##				        current class
    def get_log_file(self):
	return self.file_path + self.file_name


